home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / yesorno.arc / YESORNO.C
Text File  |  1987-03-02  |  5KB  |  184 lines

  1. /* NAME
  2.  
  3.         yesorno -- Get a yes or no answer to a question
  4.  
  5.     SYNOPSIS
  6.  
  7.         answer = yesorno(question)
  8.  
  9.         int answer;                                TRUE if yes, FALSE if no
  10.         char *question;                            Question to be asked.  Ends
  11.                                                     with a question mark.
  12.  
  13.     DESCRIPTION
  14.  
  15.             yesorno is a Lattice C, Version 3.11 function.  It writes an
  16.         error message on the next line and re-requests input until it
  17.         receives either a 'Y', a 'y', a 'N' or a 'n' answer.
  18.             It uses IBM BIOS interrupt 0x16 for input and stderr for output,
  19.         redirection does no affect it.
  20.             By changing the test inside the parenthesis of VALID_ANS to
  21.         another test for legal characters and the initialization of the static
  22.         character array error_message to a different one-line message, you
  23.         can use the same basic function for other tests.
  24.  
  25.     CAUTIONS
  26.  
  27.             As written, it uses the REGS union from Lattice's include file
  28.         dos.h and the Lattice function int86, neither of which may be
  29.         portable to another compiler.
  30.  
  31.     LIBRARIES USED
  32.  
  33.         None.
  34.  
  35.     AUTHOR:             Lew Paper
  36.     DATE WRITTEN :
  37.  
  38. */
  39.  
  40. #include <stdio.h>
  41. #include <dos.h>
  42. #include <string.h>
  43.  
  44. #define VIDEO_FUNC(INREGS,OUTREGS)    (void) int86(0x10, &INREGS, &OUTREGS)
  45. #define VIDEO_INT        VIDEO_FUNC(in, out)
  46.  
  47. #define VALID_ANS (answer == 'Y' || answer == 'N')
  48.  
  49. yesorno(question)
  50.     char *question;
  51.  
  52. {
  53.     static char error_message[] = "Y(es) or N(o) only please";
  54.     static int error_length = 0;            /* length of error_message - 1*/
  55.     int ans_loc;                                /* location of answer */
  56.     static int error_loc = 0;                /* location of error message */
  57.     int answer;
  58.  
  59.     union REGS in, out;                        /* Registers without segments */
  60.  
  61. #ifdef DEBUG
  62.     int i;
  63. #endif                                            /* #ifdef DEBUG */
  64.  
  65.     if (!error_length)                        /* Use error_length for clearing */
  66.                                                     /* to fit all screen widths        */
  67.         error_length = strlen(error_message) - 1;
  68.  
  69.     /* Set BH of in to current active screen page.  Note the reversal */
  70.     /* of roles between in and out.                                             */
  71.         out.h.ah = 0x0f;                        /* Get current display mode */
  72.         VIDEO_FUNC(out, in);
  73.  
  74.     fputs(question, stderr);                /* Display question */
  75.     fputc(' ', stderr);                        /* Add a space to separate answer */
  76.  
  77.     /* Set ans_loc to cursor column for answer */
  78.         in.h.ah = 3;                            /* Get cursor location */
  79.         VIDEO_INT;                                /* Sets DL to column */
  80.         ans_loc = out.h.dl;
  81.  
  82.     answer = 0;
  83.  
  84.     do
  85.     {
  86.         /* Read answer */
  87.             in.h.ah = 0x00;                        /* Read keyboard interrupt */
  88.             int86(0x16, &in, &out);
  89.             answer = toupper(out.h.al);
  90.  
  91.             fputc(answer, stderr);
  92.             fputc('\n', stderr);
  93.  
  94.         /* Set error_loc to cursor row if necessary */
  95.             if (!error_loc)
  96.             {
  97.                 in.h.ah = 3;                        /* Get cursor location */
  98.                 VIDEO_INT;                            /* Sets DH to row */
  99.                 error_loc = (int) out.h.dh << 8;
  100.  
  101.                 ans_loc |= (error_loc - 0x0100);    /* Up one row from error */
  102.  
  103. #ifdef DEBUG
  104.     /* Fill all but the last position of error line with a row of asterisks */
  105.             in.h.ah = 0x0f;                        /* Get current display mode         */
  106.                                                         /* Set AH to number of columns    */
  107.                                                         /* on screen                            */
  108.             VIDEO_INT;
  109.             for (i = (int) out.h.ah - 1; i; i--)
  110.                 fputc('*', stderr);
  111.  
  112.         /* Move cursor back to start of error line */
  113.             in.h.ah = 2;                            /* Set cursor position */
  114.             in.x.dx = error_loc;
  115.             VIDEO_INT;
  116. #endif                                                /* #ifdef DEBUG */
  117.  
  118. }
  119.  
  120.         /* Clear old error message */
  121.             in.x.ax = 0x0600;                        /* Blank window */
  122.             in.x.cx = error_loc;                    /* Start of line as UL corner */
  123.             in.x.dx = error_loc | error_length;
  124.                                                         /* End of message as LR corner */
  125.             VIDEO_INT;
  126.  
  127.         if (!VALID_ANS)
  128.         {
  129.             fputs(error_message, stderr);
  130.  
  131.             /* Move cursor to ans_loc */
  132.                 in.h.ah = 02;                        /* Set cursor position */
  133.                 in.x.dx = ans_loc;
  134.                 VIDEO_INT;
  135.         }
  136.  
  137.     } while (!VALID_ANS);
  138.  
  139.     return(answer == 'Y');
  140.  
  141. }
  142.  
  143. #ifdef DEBUG
  144. main(argc, argv)
  145.     int argc;
  146.     char *argv[];
  147.  
  148. {
  149.     union REGS min, mout;
  150.     static int q_row = 0;
  151.     int ans;
  152.  
  153.     mout.h.ah = 0x0f;                            /* Read display mode */
  154.     VIDEO_FUNC(mout, min);                    /* Set active page */
  155.  
  156.     min.x.ax = 0x0600;                        /* Clear screen */
  157.     min.x.cx = 0;
  158.     min.x.dx = 0x2479;
  159.     VIDEO_FUNC(min, mout);
  160.  
  161.     if (argc >= 3)
  162.         q_row = (atoi(argv[2]) << 8);
  163.         min.h.ah = 2;                            /* Set cursor position */
  164.         min.x.dx = q_row;
  165.         VIDEO_FUNC(min, mout);
  166.  
  167.     if (argc >= 2)
  168.         ans = yesorno(argv[1]);
  169.     else
  170.         ans = yesorno("Do you want to end testing yesorno?");
  171.     fputs((ans ? "Yes" : "No"), stderr);
  172.  
  173.         min.h.ah = 2;                            /* Set cursor position */
  174.         min.x.dx = (q_row < (13 << 8) ? q_row + 0x0800 : q_row - 0x0800);
  175.         VIDEO_FUNC(min, mout);
  176.  
  177.         if (argc == 1 || argc > 3)
  178.         fputs("USAGE yesorno [question [row]] WHERE row IS 0-24\n", stderr);
  179.  
  180. }
  181.  
  182. #endif                                                    /* #ifdef DBUG */
  183.  
  184.